<?php
declare (strict_types=1);

namespace app\user\service\user\admin;

use app\common\service\BaseService;
use app\course\logic\cert\admin\CourseCert;
use eduline\admin\libs\pageform\FormItem;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageList;

/**
 * 证书
 */
class Cert extends BaseService
{
    /**
     * 学员-课程证书
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-08-07
     */
    public function studentCourse(int $userId)
    {
        $map = [];
        if (input('?get.course_title')) {
            $title = trim(input('get.course_title/s'), ' ');
            $title && $map[] = ['course.title', 'like', '%' . $title . '%'];
        }
        // 查询当前课程已经学完的所有学员
        /** @var CourseCert $certLogic */
        $certLogic = app(CourseCert::class);
        $students  = $certLogic->getUserCerts($map, $userId);

        // 定义字段
        $keyList = [
            'course_title' => ListItem::make()->title('课程'),
            'create_time'  => ListItem::make()->title('学完时间'),
        ];

        $searchKey = [
            'course_title' => FormItem::make('input')->title(false)->placeholder('课程标题')->maxlength(30),
        ];

        // 设置表单
        $list = app(PageList::class);
        // 表单字段
        $list->pageKey   = $keyList;
        $list->searchKey = $searchKey;
        $list->datas     = $students;

        return $list->send();
    }

}
